#pragma once
#include <iostream>

using namespace std;
#include <vector>
#include<functional>
#include <assert.h>


namespace zsw
{
    template <class T>
    class Less
    {
    public:
        bool operator()(const T x, const T y)
        {
            return x < y;
        }
    };



    template <class T, class Container = vector<T>, class Compare = Less<T>>
    class priority_queue
    {
    public:
        priority_queue()
        {}

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first < last)
            {
                push(*first++);
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        const T& top() const
        {
            assert(c.size() > 0);

            return c.front();
        }

        void push(const T& x)
        {
            c.push_back(x);
            adjust_up(c.size() - 1);
        }

        void pop()
        {
            swap(c[0], c[c.size() - 1]);
            c.pop_back();
            adjust_down(0);
        }

    private:

        void adjust_up(size_t child)
        {
            size_t parent = (child - 1) / 2;

            while (child > 0)
            {
                //if (c[parent] < c[child])
                if (comp(c[parent], c[child]))
                    swap(c[parent], c[child]);
                else
                    break;

                child = parent;
                parent = (child - 1) / 2;
            }
        }

        void adjust_down(size_t parent)
        {
            size_t child = parent * 2 + 1;

            while (child < c.size())
            {
                if (child + 1 < c.size() && comp(c[child], c[child + 1]))
                    child++;

                if (comp(c[parent], c[child]))
                    swap(c[parent], c[child]);
                else
                    break;

                parent = child;
                child = child * 2 + 1;
            }
        }

        Container c;

        Compare comp;

    };

};